import { ObjInfo, Types } from '@amcax/base';
import { featureCommandList } from '@app-cad/common/config/featureCommandList';
import { omit } from 'lodash';
import { commonCommandList } from './commonCommandList';
import { curvedCommandList } from './curveCommandList';
import { CommandConfigItem } from './types';

const getEditCommand = (objInfo: ObjInfo): CommandConfigItem => {
  const objectType = objInfo?.type;

  const editableTypesWithoutParams = [
    Types.DESIGN_TABLE,
    Types.DESIGN_TABLE_CONFIGURATION,
  ];

  if (
    objectType == null ||
    (!editableTypesWithoutParams.includes(objectType) &&
      objInfo?.params == null) ||
    (objectType === Types.DESIGN_TABLE && objInfo?.table == null)
  ) {
    return null;
  }

  const findResult = (v: CommandConfigItem, params: any) => {
    const item = omit(
      v,
      'objType',
      'command',
      'notApplyIconToLeftPanel',
      'noEdit',
    );

    return Object.keys(item).every((key) => item[key] === params[key]);
  };

  const resultFromList = [
    ...commonCommandList,
    ...curvedCommandList,
    ...featureCommandList,
  ].find((v) => v.objType === objInfo.type && findResult(v, objInfo.params));

  if (resultFromList != null) {
    return resultFromList;
  }

  return null;
};

export default getEditCommand;
